#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn = 1000005;

long long n,k;

struct node{
    long long v;
    int id;
}a[maxn];
deque<node> q;
deque<node> p;

int main(){
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>a[i].v;
        a[i].id=i;
    }

    for(int i=1;i<=k-1;i++){
    	while(!q.empty() && q.back().v>a[i].v)q.pop_back();
        q.push_back((node){a[i].v,i}); 
        while(!p.empty() && p.back().v<a[i].v)p.pop_back();
        p.push_back((node){a[i].v,i}); 
    }
    for(int i=k;i<=n;i++){
    	while(!q.empty() && q.back().v>a[i].v)q.pop_back();
        q.push_back((node){a[i].v,i}); 
        while(!q.empty() && q.front().id<i-k+1)q.pop_front();
       	cout<<q.front().v<<" "; 
    }
    cout<<endl;
 	for(int i=k;i<=n;i++){
    	while(!p.empty() && p.back().v<a[i].v)p.pop_back();
        p.push_back((node){a[i].v,i}); 
        while(!p.empty() && p.front().id<i-k+1)p.pop_front();
       	cout<<p.front().v<<" "; 
    }    
}
